package kin.system.util;







import java.io.File;

import java.io.StringWriter;



import java.io.IOException;



import java.io.Writer;

import java.net.URISyntaxException;

import java.util.Arrays;
import java.util.Collection;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

import javax.tools.JavaCompiler;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager.Location;




import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

import freemarker.template.TemplateException;

public class GenObjectUtile {


public void init (){
	Map root = new HashMap();
    
	root.put("class", "Cbb");

	Collection properties = new HashSet();

	root.put("properties", properties);

	 //subfield 1: currency 

	Map id = new HashMap();

	id.put("name", "id");

	id.put("type", "String");

	properties.add(id);

	 //subfield 2: amount 

	Map name = new HashMap();

	name.put("name", "name");

	name.put("type", "String");

	properties.add(name);
}
	
	
	
/* ------------------------------------------------------------------- */

/* You usually do it only once in the whole application life-cycle: */

/* Create and adjust the configuration */
	/*System.out.println(ClassLoader.getSystemResource("").getFile());
	
	File f=new File(ClassLoader.getSystemResource("").getPath()+"/userClass");
	if(!f.exists()){
		f.createNewFile();
	}
	
	System.out.print(f.getPath());*/
	/*String s=System.getProperty("user.dir");
	System.out.print(s);*/
	
	public static void v(String ftlPath,String ftlName,Map root) throws IOException, TemplateException{
		
Configuration cfg = new Configuration();

File f=new File(ClassLoader.getSystemResource("").getPath()+"/ftl/");
if(!f.mkdirs()){
	f.createNewFile();
}

cfg.setDirectoryForTemplateLoading(f);


cfg.setObjectWrapper(new DefaultObjectWrapper());

// ------------------------------------------------------------------- 

 //You usually do these for many times in the application life-cycle: 

// Get or create a template 


Template temp = cfg.getTemplate("java1.ftl", "UTF-8");

    



 Writer out = new StringWriter(); 
  
temp.process(root, out);


JavaCompiler compiler = ToolProvider.getSystemJavaCompiler ();

System.out.print(compiler);
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null , null , null );




Location location = StandardLocation.CLASS_OUTPUT ;
File fw=new File(ClassLoader.getSystemResource("").getPath()+"");

File[] outputs = new File[]{fw};


try {

fileManager.setLocation(location, Arrays.asList (outputs));

} catch (IOException e1) {

e1.printStackTrace();

}

StringSourceJavaObject sourceObject = null;
try {
	sourceObject = new StringSourceJavaObject((String) root.get("class"), out.toString());
} catch (URISyntaxException e) {
	
	e.printStackTrace();
}

Iterable< ? extends SimpleJavaFileObject> fileObjects = Arrays.asList (sourceObject);

CompilationTask task = compiler.getTask(null , fileManager, null , null , null , fileObjects);

boolean result = task.call();

if (result) {

System.out .println("jjjjj");

}

out.flush();

out.close();



	}


}

